package com.wswy.wzcx.widget.home

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.Space
import android.widget.TextView
import com.blankj.utilcode.util.SizeUtils
import com.facebook.drawee.view.SimpleDraweeView
import com.google.android.flexbox.AlignItems
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayout
import com.wswy.wzcx.R
import com.wswy.wzcx.model.home.ExtModule
import com.wswy.wzcx.model.home.LabelModel
import com.wswy.wzcx.module.base.getColor
import com.wswy.wzcx.ui.other.ClickUtils
import com.wswy.wzcx.widget.OnItemClickListener
import timber.log.Timber

class HomeExtSModulesLayout : RelativeLayout, View.OnClickListener {


    constructor(context: Context) : this(context, null)

    constructor(context: Context, attribute: AttributeSet?) : this(context, attribute, 0)

    constructor(context: Context, attribute: AttributeSet?, defStyle: Int) : super(context, attribute, defStyle) {

    }

    init {

        setPadding(SizeUtils.dp2px(16f), 0, SizeUtils.dp2px(16f), SizeUtils.dp2px(16f))

        tvTitle = TextView(context).apply {
            id = R.id.tv_title
            textSize = 20f
            setTextColor(getColor(R.color.textColorPrimary))
        }.also {
            addView(it)
        }

        flexboxLayout = FlexboxLayout(context).apply {
            flexWrap = FlexWrap.WRAP
        }.also {
            addView(it, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT).apply {
                topMargin = SizeUtils.dp2px(16f)
                addRule(BELOW, R.id.tv_title)
            })
        }

    }

    var itemDataOnItemClickListener: OnItemClickListener<ExtModule>? = null
    private lateinit var flexboxLayout: FlexboxLayout
    private lateinit var tvTitle: TextView


    fun showData(labelModel: LabelModel, datas: List<ExtModule>) {

        Timber.e(datas.toString())

        tvTitle.text = labelModel.title
        flexboxLayout.removeAllViews()
        flexboxLayout.alignItems = AlignItems.FLEX_START

        if (labelModel.style == 1) {
            showWith3(labelModel, datas)
        } else {
            showWith2(labelModel, datas)
        }

    }


    private fun showWith2(labelModel: LabelModel, datas: List<ExtModule>) {

        val rowHeight = SizeUtils.dp2px(75f)

        var hasNewRow = false
        datas.forEachIndexed { index, extModule ->

            val inflate = buildItem(extModule)

            val lp = FlexboxLayout.LayoutParams(0,rowHeight)

            Timber.e("----${extModule.title}   ${extModule.weight}")

            lp.flexGrow = 1f

            if (index > 0 && index % 2 == 0) {
                lp.isWrapBefore = true
                hasNewRow = true
            }

            if (index % 2 == 0) {
                lp.rightMargin = SizeUtils.dp2px(5f)
            } else {
                lp.leftMargin = SizeUtils.dp2px(5f)
            }

            if (hasNewRow){
                lp.topMargin = SizeUtils.dp2px(10f)
            }

            flexboxLayout.addView(inflate, lp)

        }

        val remain = datas.size % 2
        if (remain > 0) {
            val lp = FlexboxLayout.LayoutParams(0, rowHeight).apply {
                topMargin = SizeUtils.dp2px(10f)
                leftMargin = SizeUtils.dp2px(5f)
            }
            lp.flexGrow = 1f
            flexboxLayout.addView(Space(context), lp)
        }

    }

    private fun showWith3(labelModel: LabelModel, datas: List<ExtModule>) {
        // 1 0 0
        // 0 0 1

        //三个样式的

        val rowHeightMax = SizeUtils.dp2px(158f)

        val context = context

        val orders = arrayOf(1, 2, 3)

        if (datas.first().weight == 1) {
            orders[0] = 1
            orders[1] = 2
            orders[2] = 3
        } else if (datas.first().weight == 0){
            orders[0] = 3
            orders[1] = 2
            orders[2] = 1
        }

        //大图
        val maxLayout = RelativeLayout(context).apply {

        }.also {
            flexboxLayout.addView(it, FlexboxLayout.LayoutParams(0, rowHeightMax).apply {
                flexGrow = 1f
                order = orders[0]
            })
        }

        //小图
        val stackLayout = LinearLayout(context).apply {
            orientation = LinearLayout.VERTICAL
        }.also {
            flexboxLayout.addView(it, FlexboxLayout.LayoutParams(0, rowHeightMax).apply {
                flexGrow = 1f
                order = orders[2]
            })
        }
        flexboxLayout.addView(Space(context), FlexboxLayout.LayoutParams(SizeUtils.dp2px(10f), rowHeightMax).apply {
            order = orders[1]
        })


        var firstM = false
        if (orders[0] == 1) {
            //第一个是大图
            //小图
            datas.forEachIndexed { index, extModule ->

                val inflate = buildItem(extModule)
                if (index == 0) {
                    extractLP(inflate)
                    maxLayout.addView(inflate)
                } else {
                    stackLayout.addView(inflate, LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0).apply {
                        weight = 1f
                        if (firstM) {
                            topMargin = SizeUtils.dp2px(10f)
                        }
                        firstM = true
                    })
                }
            }

        } else {
            firstM = false
            datas.forEachIndexed { index, extModule ->

                val inflate = buildItem(extModule)
                if (index != datas.size - 1) {
                    stackLayout.addView(inflate, LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0).apply {
                        weight = 1f
                        if (firstM) {
                            topMargin = SizeUtils.dp2px(10f)
                        }
                        firstM = true
                    })
                } else {
                    extractLP(inflate)
                    maxLayout.addView(inflate)

                }
            }
        }

    }

    private fun extractLP(layout:View){
        (layout as? ConstraintLayout)?.let {
            //大图修改样式

            val subTitle = it.findViewById<View>(R.id.tv_sub_title)

            val space = it.findViewById<View>(R.id.space)
            (subTitle.layoutParams as? ConstraintLayout.LayoutParams)?.let {

                it.bottomToBottom = R.id.space
                it.bottomMargin = SizeUtils.dp2px(5f)
            }

        }
    }

    private fun buildItem(extModule: ExtModule): View {
        val inflate = LayoutInflater.from(context).inflate(R.layout.item_layout_home_ex_modules, this, false)

        inflate.findViewById<TextView>(R.id.tv_title).text = extModule.title
        inflate.findViewById<TextView>(R.id.tv_sub_title).text = extModule.subTitle
        inflate.findViewById<SimpleDraweeView>(R.id.img_bg)?.let {
            it.setImageURI(extModule.image)
        }
        inflate.tag = extModule
        inflate.setOnClickListener(this)

        return inflate
    }


    override fun onClick(v: View?) {
        if (ClickUtils.isFastClick()) {
            return
        }

        if (itemDataOnItemClickListener != null) {
            (v?.tag as? ExtModule)?.let {
                itemDataOnItemClickListener?.onItemClick(v, it)
            }

        }
    }

}